<?php


namespace app\admin\controller;


use app\common\controller\Adminbase;
use think\Db;
use think\facade\Session;

class Cwrite extends Adminbase
{
    public function index()
    {
        $this->admin1 = Session::get("admin");
        if ($this->admin1['roleid'] == 3) {
            $where[] = ['id', '=', $this->admin1['id']];
        }
        $where2=array();
        if ($this->admin1['roleid'] == 4) {
            $where2[] = ['id', '=', $this->admin1['id']];
            $where[] = ['area_id', '=', $this->admin1['area_id']];
        }
        //todo 设置权限以后做
        $where1[] = ['is_del', '=', 0];
        $where[] = ['roleid', '=', 3];
        $where1[] = ['status', '=', 1];
        $list = array();
        $count = 0;
        $feng_list = Db::name('admin')->field('id,nickname as catname,roleid,area_id')
            ->order('area_id')
            ->where($where1)
            ->where($where)
            ->select();
        foreach ($feng_list as $k => $v) {
            $list[$count]['parentid'] = 0;
            $list[$count]['target'] = 'right';
            $list[$count]['id'] = $v['id'];
            $list[$count]['catname'] = $v['catname'];
            $list[$count]['isParent'] = true;
            $count++;
            $son = Db::name('admin')
                ->field('id,nickname as catname,roleid,area_id')
                ->where($where1)
                ->where($where2)
                ->where('roleid', '=', 4)
                ->where('area_id', '=', $v['area_id'])
                ->select();
            if ($son) {
                foreach ($son as $k2 => $v2) {
                    $list[$count]['parentid'] = $v['id'];
                    $list[$count]['target'] = 'right';
                    $list[$count]['id'] = $v2['id'];
                    $list[$count]['catname'] = $v2['catname'];
                    $list[$count]['isParent'] = true;
                    $count++;
                    $business_list = Db::name('business')->where('belong_admin_id', '=', $v2['id'])->select();
                    if ($business_list) {
                        foreach ($business_list as $k3 => $v3) {
                            $list[$count]['parentid'] = $v2['id'];
                            $list[$count]['target'] = 'right';
                            $list[$count]['id'] = $count;
                            $list[$count]['catname'] = $v3['business_name'];
                            $list[$count]['url'] = url('detail', ['business_id' => $v3['id']]);
                            $count++;
                        }
                    }
                }
            }
        }
        $this->assign('json', json_encode($list));

        return $this->fetch('cwrite/index');
    }

    public function detail()
    {
        $data = $this->request->param();
        if ($this->request->isAjax()) {
            $business_id=$data['business_id'];
            $business_info=Db::name('business')->find($business_id);
            $months_list=getAllMonths($business_info['create_time'],time());
            $months_list = array_reverse($months_list);
            $data_list = array();
            foreach ($months_list as $k=>$v){
                $dataScope=explode('--',$v);
                $dataScope=getMonthTimestampRange($dataScope[0],$dataScope[1]);
                $data_list[$k]['id']=$k+1;
                $data_list[$k]['date']=$v;
                $order_ids=Db::name('order')
                    ->where('business_id','=',$business_id)
                    ->where('pay_time','between',[$dataScope[0],$dataScope[1]])
                    ->where('status','=',3)
                    ->column('id');
                $order_detail_sum=Db::name('order_detail')
                    ->where('order_id','in',$order_ids)
                    ->sum('write_price');
                $data_list[$k]['count_amount']=$order_detail_sum;
                $data_list[$k]['is_kai']=0;
                $data_list[$k]['business_id']=$business_id;
                $bc_info=Db::name('business_cwrite')
                    ->where('business_id','=',$business_id)
                    ->where('date','=',$v)
                    ->find();
                if ($bc_info){
                    $data_list[$k]['is_kai']=1;
                }
                //id  商家名称 月份 总额 是否开票
            }
            $result = ["code" => 0, "count" => count($months_list), "data" => $data_list];
            return json($result);
        }
        $this->assign('business_id', $data['business_id']);
        return $this->fetch();
    }
    public function detail2(){
        $data = $this->request->param();
        if ($this->request->isAjax()) {
            $goods_where[] = ['is_del', '=', 0];
            $_list = Db::name('goods')
                ->order(array('id' => 'desc'))
                ->where($goods_where)
                ->select();
            $dataScope=explode('--',$data['date']);
            $dataScope=getMonthTimestampRange($dataScope[0],$dataScope[1]);
            $order_ids = Db::name('order')
                ->where('business_id', '=', $data['business_id'])
                ->where('pay_time', 'between', [$dataScope[0], $dataScope[1]])
                ->where('status', '=', 3)
                ->column('id');
            $order_detail_where[] = ['order_id', 'in', $order_ids];
            foreach ($_list as $k => $v) {
                //总核销价格
                $write_price_sum = Db::name('order_detail')
                    ->where('goods_id', $v['id'])
                    ->where($order_detail_where)
                    ->sum('write_price');
                //销售总数量
                $count_sum = Db::name('order_detail')
                    ->where('goods_id', $v['id'])
                    ->where($order_detail_where)
                    ->sum('goods_num');
                $_list[$k]['write_price'] = $v['write_price'];
                $_list[$k]['write_price_sum'] = $write_price_sum;
                $_list[$k]['count_sum'] = $count_sum;
            }
            $result = ["code" => 0, "count" => count($_list), "data" => $_list];
            return json($result);
        }
        $this->assign('business_id',$data['business_id']);
        $this->assign('date',$data['date']);
        return $this->fetch('detail2');
    }
    public function  kai(){
        $data = $this->request->param();
        $insert_data=array(
            'business_id'=>$data['business_id'],
            'date'=>$data['date'],
            'create_time'=>time(),
            'count_amount'=>$data['count_amount']
        );
        $result=Db::name('business_cwrite')->insertGetId($insert_data);
        if ($result){
            return ['code' => 200, 'data' => '', 'msg' => '操作成功'];
        }else{
            return ['code' => 100, 'data' => '', 'msg' => '操作失败'];
        }
    }
}